package com.OA.controller;


import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.OA.entity.DepartMentName;
import com.OA.entity.Log;
import com.OA.entity.LogComment;
import com.OA.entity.LogType;
import com.OA.entity.User;
import com.OA.entity.UserDept_Vo;
import com.OA.service.Log_CY_service;
import com.OA.util.Page;
import com.alibaba.fastjson.JSON;

import sun.print.PageableDoc;

@Controller
@RequestMapping("log")
public class Log_CY_controller {

	@Autowired
	private Log_CY_service logService;
	
	@Autowired
	public static Map<String, Object> map = new HashMap<String, Object>();
	 
	/**
	 * 加载指定用户的日志
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @param resp
	 * @throws IOException
	 * @throws ParseException 
	 */
	@RequestMapping(value="loadAllMyLogById.do")
	public void loadAllMyLogById(int pageNo,int pageSize,
			Integer logType,String keyword,String fromDate,String toDate,
			HttpServletRequest req,HttpServletResponse resp) throws IOException, ParseException{
		resp.setCharacterEncoding("utf-8");
		int userId = Integer.parseInt(req.getParameter("userId"));
		map.put("beginIndex", (pageNo-1)*pageSize);
		map.put("pageNo", pageNo);
		map.put("pageSize", pageSize);
		map.put("userId", userId);
		map.put("logType", logType);
		SimpleDateFormat simple = new SimpleDateFormat("yyyy-mm-dd");
		Date fDate = null;
		Date tDate = null;
		if(fromDate!=""){
			fDate = simple.parse(fromDate);
			tDate = simple.parse(toDate);
			map.put("fromDate", fDate);
			map.put("toDate", tDate);
		}else{
			map.put("fromDate", fromDate);
			map.put("toDate", toDate);
		}
		if(keyword!=""){
			map.put("keyword", "%"+keyword+"%");
		}else{
			map.put("keyword", null);
		}
		Page<Log> page = logService.loadAllMyLogById(map);
		resp.getWriter().write(JSON.toJSONString(page));
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	
	/**
	 * 加载日志的类型
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	@RequestMapping(value="loadLogType.do")
	public void loadLogType(HttpServletRequest req,HttpServletResponse resp) throws IOException{
		resp.setCharacterEncoding("utf-8");
		List<LogType> list = logService.loadLogType();
		
		resp.getWriter().write(JSON.toJSONString(list));
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	
	/**
	 * 加载所有部门的名称
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	@RequestMapping(value="loadDepartmentName.do")
	public void loadDepartmentName(HttpServletRequest req,HttpServletResponse resp) throws IOException{
		resp.setCharacterEncoding("utf-8");
		List<DepartMentName> list = logService.loadDepartmentName();
		resp.getWriter().write(JSON.toJSONString(list));
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	
	/**
	 * 加载该用户能看的其他用户的日志
	 * @param pageNo
	 * @param pageSize
	 * @param logType
	 * @param keyword
	 * @param fromDate
	 * @param toDate
	 * @param req
	 * @param resp
	 * @throws IOException
	 * @throws ParseException
	 */
	@RequestMapping(value="loadOtherCanSee.do")
	public void loadOtherCanSee(int pageNo,int pageSize,
			Integer logType,String keyword,String fromDate,String toDate,Integer deptId,
			HttpServletRequest req,HttpServletResponse resp) throws IOException, ParseException{
		resp.setCharacterEncoding("utf-8");
		int userId = Integer.parseInt(req.getParameter("userId"));
		map.put("beginIndex", (pageNo-1)*pageSize);
		map.put("pageNo", pageNo);
		map.put("pageSize", pageSize);
		map.put("userId", userId);
		map.put("logType", logType);
		map.put("deptId", deptId);
		SimpleDateFormat simple = new SimpleDateFormat("yyyy-mm-dd");
		Date fDate = null;
		Date tDate = null;
		if(fromDate!=""){
			fDate = simple.parse(fromDate);
			tDate = simple.parse(toDate);
			map.put("fromDate", fDate);
			map.put("toDate", tDate);
		}else{
			map.put("fromDate", fromDate);
			map.put("toDate", toDate);
		}
		if(keyword!=""){
			map.put("keyword", "%"+keyword+"%");
		}else{
			map.put("keyword", null);
		}
		int count = logService.logCountCanSee(map);
		Page<Log> page = logService.loadOtherCanSee(map);
		resp.getWriter().write(JSON.toJSONString(page));
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	
	/**
	 * 查看日志内容
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="seeLog.do")
	public String seeLog(HttpServletRequest req,HttpServletResponse resp) throws IOException{
		resp.setCharacterEncoding("utf-8");
		req.setCharacterEncoding("utf-8");
		int logId = Integer.parseInt(req.getParameter("logId"));
		Log log = logService.loadLogById(logId);
		req.setAttribute("log", log);
		System.out.println(log.getLogContext());
		return "logJsp_CY/logContext";
	}
	
	/**
	 * 加载评论
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @param resp
	 * @throws IOException
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping(value="loadComments.do")
	public void loadComments(int logId,int pageNo,int pageSize,HttpServletRequest req,HttpServletResponse resp) throws IOException, ParseException{
		resp.setCharacterEncoding("utf-8");
		map.put("logId", logId);
		map.put("beginIndex", (pageNo-1)*pageSize);
		map.put("pageNo", pageNo);
		map.put("pageSize", pageSize);
		Page<LogComment> page = logService.loadComments(map);
		resp.getWriter().write(JSON.toJSONString(page));
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	
	/**
	 * 用户提交评论
	 * @param comment
	 * @param req
	 * @param resp
	 * @throws IOException
	 * @throws ParseException
	 */
	@RequestMapping(value="commitComment.do")
	public void commitComment(String comment,HttpServletRequest req,HttpServletResponse resp) throws IOException, ParseException{
		resp.setCharacterEncoding("utf-8");
		map.put("comment", comment);
		logService.commitComment(map);
		resp.getWriter().write("{\"msg\":\"ok\"}");
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	
	/**
	 * 展示这个日志可以查看的用户
	 * @param logId
	 * @param req
	 * @param resp
	 * @throws IOException
	 * @throws ParseException
	 */
	@RequestMapping(value="log_permissionUser.do")
	public void log_permissionUser(int logId,HttpServletRequest req,HttpServletResponse resp) throws IOException, ParseException{
		resp.setCharacterEncoding("utf-8");
		List<Log> list = logService.log_permissionUser(logId);
		resp.getWriter().write(JSON.toJSONString(list));
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	
	/**
	 * 展示没有权限查看这篇日志用户
	 * @param logId
	 * @param req
	 * @param resp
	 * @throws IOException
	 * @throws ParseException
	 */
	@RequestMapping(value="log_permissionOutUser.do")
	public void log_permissionOutUser(int logId,HttpServletRequest req,HttpServletResponse resp) throws IOException, ParseException{
		resp.setCharacterEncoding("utf-8");
		List<UserDept_Vo> list = logService.log_permissionOutUser(logId);
		resp.getWriter().write(JSON.toJSONString(list));
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	
}
